﻿using System;
using System.Data;
using System.Text;

namespace PeisongWeb.admin
{
    public partial class iframe_productPriceV2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                amstockBind();
            }
        }

        private void amstockBind()
        {
            int productId = 0;
            int.TryParse(Request.QueryString["productId"], out productId);

            if (productId != 0)
            {
                DataSet ds = logic.priceSeller.getPriceSellerAmstock(productId);

                StringBuilder chart_settings = new StringBuilder(GenerateHtml(ds.Tables[0]));

                StringBuilder data_file = new StringBuilder();
                foreach (DataRow row in ds.Tables[1].Rows)
                {
                    data_file.AppendFormat("{0}\\n", row["Item"]);
                }

                chart_settings.Replace("#data#", data_file.ToString());

                StringBuilder script = new StringBuilder("<script type='text/javascript'>");
                script.Append(@"var so = new SWFObject(""../amstock/amstock.swf"", ""amstock"", ""900"", ""500"", ""8"", ""#FFFFFF"");");
                script.Append(@"so.addParam(""wmode"",""transparent"");");
                script.AppendFormat(@"so.addVariable(""chart_settings"", encodeURIComponent(""{0}""));", chart_settings.ToString());
                script.Append(@"so.addVariable(""data_file"", encodeURIComponent(""amstock/amstock_data.null""));");
                script.Append(@"so.write(""flashcontent"");");
                script.Append("</script>");

                scriptLayout.Text = script.ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt">供应商列表</param>
        /// <returns></returns>
        private string GenerateHtml(DataTable dt)
        {
            StringBuilder append = new StringBuilder();
            append.Append("<settings>");

            append.Append("<strings>");
            append.Append("<processing_data>正在处理数据</processing_data>");
            append.Append("<loading_data>正在加载数据</loading_data>");
            append.Append("<wrong_date_format>输入的日期格式不正确</wrong_date_format>");
            append.Append("<no_data>当前没有数据</no_data>");

            append.Append("<months>");
            append.Append("<jan>1</jan>");
            append.Append("<feb>2</feb>");
            append.Append("<mar>3</mar>");
            append.Append("<apr>4</apr>");
            append.Append("<may>5</may>");
            append.Append("<jun>6</jun>");
            append.Append("<jul>7</jul>");
            append.Append("<aug>8</aug>");
            append.Append("<sep>9</sep>");
            append.Append("<oct>10</oct>");
            append.Append("<nov>11</nov>");
            append.Append("<dec>12</dec>");
            append.Append("</months>");

            append.Append("<weekdays>");
            append.Append("<sun>日</sun>");
            append.Append("<mon>一</mon>");
            append.Append("<tue>二</tue>");
            append.Append("<wed>三</wed>");
            append.Append("<thu>四</thu>");
            append.Append("<fri>五</fri>");
            append.Append("<sat>六</sat>");
            append.Append("</weekdays>");

            append.Append("</strings>");

            append.Append("<margins>0</margins>");
            append.Append("<font>Tahoma</font>");
            append.Append("<redraw>true</redraw>");
            append.Append("<number_format>");
            append.Append("<decimal_separator>.</decimal_separator>");
            append.Append("<thousand_separator>,</thousand_separator>");
            append.Append("</number_format>");

            append.Append("<data_sets>");

            append.Append("<data_set did='0'>");
            append.Append("<color>006600</color>");
            append.Append("<csv>");
            append.Append("<reverse>false</reverse>");
            append.Append("<separator>,</separator>");
            append.Append("<date_format>YYYY-MM-DD</date_format>");
            append.Append("<decimal_separator>.</decimal_separator>");
            append.Append("<columns>");
            append.Append("<column>date</column>");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                append.AppendFormat("<column>{0}</column>", "sp" + i);
            }
            append.Append("</columns>");
            append.Append("<data>#data#</data>");
            append.Append("</csv>");
            append.Append("</data_set>");

            append.Append("</data_sets>");

            append.Append("<charts>");

            append.Append("<chart cid='first'>");
            append.Append("<height>60</height>");
            append.Append("<title>价格</title>");
            append.Append("<border_color>#CCCCCC</border_color>");
            append.Append("<border_alpha>100</border_alpha>");

            append.Append("<grid>");
            append.Append("<x>");
            append.Append("<color>006600</color>");
            append.Append("<alpha>40</alpha>");
            append.Append("<approx_count>10</approx_count>");
            append.Append("</x>");
            append.Append("<y_left>");
            append.Append("<color>006600</color>");
            append.Append("<alpha>40</alpha>");
            append.Append("<approx_count>10</approx_count>");
            append.Append("</y_left>");
            append.Append("<y_right>");
            append.Append("<enabled>0</enabled>");
            append.Append("</y_right>");
            append.Append("</grid>");

            append.Append("<values>");
            append.Append("<x>");
            append.Append("<bg_color>EEEEEE</bg_color>");
            append.Append("</x>");
            append.Append("</values>");


            append.Append("<legend>");
            append.Append("<text_color>FF0000</text_color>");
            append.Append("<show_date>true</show_date>");
            append.Append("</legend>");

            append.Append("<column_width>100</column_width>");

            append.Append("<graphs>");

            String[] colors = new String[] { "FF0000", "006600", "E7E7E7", "7F8DA9", "039202", "7c0292", "026e92", "022492", "7f9202", "925002" };
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                append.AppendFormat("<graph gid='{0}'>", "sp" + i);
                //append.Append("<color>FFFFFF</color>");
                //append.Append("<fill_alpha>50</fill_alpha>");
                append.AppendFormat("<title>{0}</title>", dt.Rows[i]["sellerName"]);
                append.Append("<data_sources>");
                append.AppendFormat("<close>{0}</close>", "sp" + i);
                append.Append("</data_sources>");
                append.AppendFormat("<color>{0}</color>", colors[i]);
                append.Append("<bullet>round_outline</bullet>");

                append.Append("<legend>");
                append.Append("<date key='false' title='true'><![CDATA[￥{close}]]></date>");
                //append.Append("<period key='false' title='false'><![CDATA[最低价格:<b>￥{low}</b> 最高价格:<b>￥{high}</b>]]></period>");
                append.Append("</legend>");
                append.Append("<width>2</width>");
                append.Append("</graph>");
            }

            append.Append("</graphs>");

            append.Append("</chart>");
            append.Append("</charts>");

            append.Append("<date_formats>");
            append.Append("<x_axis><days>DD日</days><months>month月</months><years>YYYY年</years></x_axis>");
            append.Append("<legend>");
            append.Append("<days>YYYY年month月DD日</days>");
            append.Append("<weeks>YYYY年month月DD日</weeks>");
            append.Append("</legend>");
            append.Append("</date_formats>");

            append.Append("<data_set_selector>");
            append.Append("<enabled>false</enabled>");
            append.Append("</data_set_selector>");

            append.Append("<period_selector>");

            append.Append("<periods>");
            append.Append("<period type='DD' count='1'>1天</period>");
            append.Append("<period type='DD' count='7'>1周</period>");
            append.Append("<period type='DD' count='10'>10天</period>");
            append.Append("<period type='DD' count='15'>15天</period>");
            append.Append("<period type='MM' count='1'>1个月</period>");
            append.Append("<period type='MM' count='2'>2个月</period>");
            append.Append("<period type='MM' count='3'>3个月</period>");
            append.Append("<period type='MM' count='6'>6个月</period>");
            append.Append("<period selected='true' type='YYYY' count='1'>1年</period>");
            //append.Append("<period type='YTD' count='0'>环比</period>");
            append.Append("<period type='MAX'>最大</period>");
            append.Append("</periods>");

            append.Append("<periods_title>区间:</periods_title>");
            append.Append("<custom_period_title>选择日期范围:</custom_period_title>");

            append.Append("</period_selector>");

            append.Append("<header>");
            append.Append("<enabled>false</enabled>");
            append.Append("</header>");

            append.Append("<scroller>");
            append.Append("<graph_data_source>sp0</graph_data_source>");
            append.Append("<resize_button_style>dragger</resize_button_style>");

            append.Append("<playback>");
            append.Append("<enabled>false</enabled>");
            append.Append("</playback>");
            append.Append("</scroller>");

            append.Append("</settings>");

            return append.ToString();
        }
    }
}
